var gulp = require('gulp')

var $ = require('gulp-load-plugins')()
var open = require('open')


//压缩 主页 html文件
gulp.task('html', function () {
    return gulp.src('./src/*.html')
        .pipe($.htmlmin({
            collapseWhitespace: true,
            minifyJS: true,
            minifyCSS:true,
            removeComments: true,
            removeSciptTypeAttributes: true,
            removeStyleLinkTypeAttributes: true
        }))
        .pipe(gulp.dest('./dist/'))
        .pipe($.connect.reload())
})

//压缩子页面的html
gulp.task('pages', function () {
    return gulp.src('./src/pages/**/*.html')
        .pipe($.htmlmin({
            collapseWhitespace: true,
            minifyJS: true,
            minifyCSS:true,
            removeComments: true,
            removeSciptTypeAttributes: true,
            removeStyleLinkTypeAttributes: true
        }))
        .pipe(gulp.dest('./dist/pages/'))
        .pipe($.connect.reload())
})

//压缩个人博客的html
gulp.task('blog', function () {
    return gulp.src('./src/blog/**/*.html')
        .pipe($.htmlmin({
            collapseWhitespace: true,
            minifyJS: true,
            minifyCSS:true,
            removeComments: true,
            removeSciptTypeAttributes: true,
            removeStyleLinkTypeAttributes: true
        }))
        .pipe(gulp.dest('./dist/blog/'))
        .pipe($.connect.reload())
})

//压缩js文件
gulp.task('js', function () {
    return gulp.src('./src/js/**/*.js')
        .pipe($.babel())
        .pipe($.uglify())
        .pipe(gulp.dest('./dist/js/'))
        .pipe($.connect.reload())
})

//压缩less文件
gulp.task('less', function () {
    return gulp.src('./src/less/**/*.less')
        .pipe($.less())
        .pipe(gulp.dest('./src/css/'))
        .pipe($.connect.reload())
})

//压缩css文件
gulp.task('css', ['less'], function () {
    return gulp.src('./src/css/**/*.css')
        .pipe($.minifyCss())
        .pipe(gulp.dest('./dist/css/'))
        .pipe($.connect.reload())
})

//压缩图片
gulp.task('img', function () {
    return gulp.src('./src/imgs/**/*.*')
        .pipe($.imagemin({progressive: true}))
        .pipe(gulp.dest('./dist/imgs/'))
        .pipe($.connect.reload())
})

//全自动
gulp.task('server', ['default'], function () {
    $.connect.server({
        root:'./dist',//监视目标文件
        livereload: true,// 实时刷新
        port: 3000
    })

    open('http://localhost:3000')

    gulp.watch('./src/*.html', ['html'])
    gulp.watch('./src/pages/**/*.html', ['pages'])
    gulp.watch('./src/blog/**/*.html', ['blog'])
    gulp.watch('./src/js/**/*.js', ['js'])
    gulp.watch('./src/less/**/*.less', ['less'])
    gulp.watch('./src/css/**/*.css', ['css'])
    gulp.watch('./src/imags/**/*.*', ['img'])
    
})

//注册一个默认的任务
gulp.task('default', ['html', 'pages', 'blog', 'css', 'less', 'js', 'img'])
